package com.xframe.common.core.service.command;


import com.xframe.common.config.CommandRunTimeArgs;
import com.xframe.common.core.constant.ErrorConstant;
import com.xframe.domain.CommandModel;
import com.xframe.common.core.dto.ErrorCode;
import com.xframe.common.core.exception.BusinessException;
import com.xframe.domain.ModelRepositoryFactory;
import com.xframe.domain.repository.SysBaseRepository;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.role.SysUser;
import com.xframe.domain.sys.SysModel;
import com.xframe.event.bus.core.model.DataMessage;
import com.xframe.event.bus.core.msg.MessageSender;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.IModel;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.util.StringUtil;

import java.lang.reflect.Method;


/**
 * @Description: 初始化用户
 * @ClassName: InitializeUser
 * @Author: lmx
 * @CreateTime: 2024/01/21
 **/
public class UnlockUserProc extends BaseCommand {
    private static String unlockFlag="0";
    @Override
    public Object ProcCommand(CommandModel requestArgs, IUserCredentials userCredentials) throws BusinessException {

        try {
            if (StringUtil.isEmpty(requestArgs.getModelCode())) {
                throw new BusinessException(ErrorCode.MODELID_NULL, ErrorConstant.MODELID_NULL_MSG);
            }
            SysModel sysModel = SysModel.getInstance(requestArgs.getModelCode());
            if (sysModel == null) {
                throw new BusinessException(ErrorCode.DB_RECORD_NOFIND,
                        StringUtil.format(ErrorConstant.DB_RECORD_NOFIND_MSG, requestArgs.getModelCode()));
            }
            SysUser sysUser = deserializationToSysUser(sysModel, requestArgs);

            if(sysUser == null){
                throw new DalFrameException("系统用户信息为null");
            }


            SysBaseRepository repository = ModelRepositoryFactory.builder(SysUser.class);
            repository.setBaseAttrVal(sysUser, userCredentials);
            sysUser.setLockFlag(unlockFlag);

            // 调用repository对象的createNew方法
            Method method = this.getMethodByName(repository.getClass(), "update", IModel.class, ITableInfo.class);
            System.out.println(method);
            this.execRepository(repository, method, sysUser, TableInfoBuilder.BuildTableInfoByAnnotation(SysUser.class));

            // 发消息到消息队列
            if(sysUser.getObjectId() != null) {
                MessageSender messageSender = MessageSender.getMessageSender();
                if(messageSender != null) {
                    DataMessage dataMessage = DataMessage.valueOf(SysUser.class.getTypeName(), DataMessage.UPDATE, sysUser.getObjectId().toString(),
                            null, sysUser, requestArgs.getSysCode(), userCredentials.getName());
                    messageSender.sendMsg(CommandRunTimeArgs.getRunTimeArgs().getDataQueueName(), dataMessage);
                }
            }
            return "OK";
        } catch (BusinessException businessException) {
            throw businessException;
        } catch (DalFrameException exception) {
            throw new BusinessException(exception.getCode(), exception.getMessage());
        } catch (Exception exception) {
            throw new BusinessException(ErrorCode.INTERNAL_SERVER_ERROR, exception.getMessage());
        }
    }


    private SysUser deserializationToSysUser(SysModel sysModel,CommandModel requestArgs){
        IModel model = deserialization(sysModel, requestArgs);
        SysUser sysUser = SysUser.createSysUser(model);
        return sysUser;
    }
    @Override
    public String getBeanName() {
        return "UnlockUser";
    }
}
